package com.qf.service.impl;

import com.qf.service.GoodsService;
import com.qf.entity.Goods;
import com.qf.mapper.GoodsDao;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class GoodsServiceImpl implements GoodsService {

    private GoodsDao goodsDao;

    public GoodsServiceImpl(GoodsDao goodsDao) {

        this.goodsDao = goodsDao;
    }

    // 图片服务器的地址
    private final String imageServerLocation = "http://localhost/images/";


    @Override
    public List<Goods> getShow() {

        List<Goods> goods = goodsDao.queryAll(null);

        goods.forEach(e ->{
            e.setSmallPic(imageServerLocation + e.getSmallPic());
        });

        return goods;

    }

    @Override
    public List<Goods> getGoodsById(Integer id) {
        return goodsDao.getGoodsById(id);
    }

    @Override
    public List<Goods> getGoodsByIds(Integer[] ids) {
        List<Goods> list = goodsDao.getGoodsByIds(ids);
        // 是将图片设置为可直接访问的地址
        list.forEach(g -> {
            String imageSrc = g.getSmallPic(); // mobile/huawei.jpg
            g.setSmallPic(imageServerLocation + imageSrc);
        });

        return list;
    }

    @Override
    public List<Goods> getGoodsByIdsNoSrc(Integer[] ids) {

        List<Goods> list = goodsDao.getGoodsByIds(ids);
        return list;
    }
//    private final String imageServerLocation = "https://decent-cat.com/";

    @Override
    public  Goods queryByGoodsId(String goodsId) {
        Goods  goods = goodsDao.queryByGoodsId(goodsId);


        return  goods;
    }

}
